A4, Code Resources
Volume Number: 9
Issue Number: 12
Column Tag: Think Top 10
Code Resources
and the A4 World
By Scott Shurr, Symantec Technical Support, Symantec Corp.
This article will describe the various kinds of code resources, and talk about the
differences between writing applications and code resources. In applications, the
machine code produced by the compiler uses the A5 register as a base address from
which to access the applications global variables. Code resources are intended to be
run simultaneously with applications, so they use a scheme designed to pr event
interference with the application which called them up. In this case the A4 register is
used as the base address for the resource’s global variables, as illustrated in the
following diagram:
It is important when writing a code resource to be aware of how and when to set
up A4. The different types of code resources require different declarations of the
main() routine. The example consists of a single segment code resource which takes 2
arguments, which are a number of beeps, and the address of a string to receive a value.
The code resource beeps the appropriate number of times, and then puts “Hello world”
into the string. Here is the first version of the code resource:
/* 1 */
/* CodeResource.c */
void CRdoBeep(int beepSize);
void main(int beepSize, char *receiveString)
{
static char sLiteral[] = "Hello world";
strcpy(receiveString,sLiteral);
CRdoBeep(beepSize);
}
void CRdoBeep(int beepSize)
{
int i;
for (i=0; i
SysBeep(beepSize);
}
Create a new project, Resource.pi, and add the CodeResource.c file and the
ANSI-A4 library to the project. Go to Set Project Type... and select the Code Resource
button. Set Name to myCodeResource, Type to CODE, and ID to 31. One of the effects of
changing the project type to anything other than Application is to cause the compiler to
use the A4 register instead of the A5 as the address of the beginning of the application
globals. Now select Build Code Resource..., and save the resource into Main.pi.rsrc.
Another project called Main.pi will be used to test the code resource, and it will expect
to find the code resource in that file in the folder. Create another new project in the
same folder as Resource.pi, and add this program to it:
/* 2 */
/* UsesCodeResource.c */
#include
typedef void (*CRPtr) (int,char*);
void main()
{
char myString[20];
Handle myCR handle;
myCRhandle = GetNamedResource('CODE',"\pmyCodeResource");
HLock(myCR handle);
(* (CRPtr) (*myCR handle))(5,myString);
HUnlock(myCR handle);
printf("myString=\"%s\"\n",myString);
}
Add UsesCodeResource.c and the ANSI library, and give it a try. The code resource
will be loaded, it will beep 5 times, and then display some garbage instead of “Hello